package com.kj.tms.system.controller.syswelcome;


import com.kj.tms.common.bean.dto.ReturnData;
import com.kj.tms.common.bean.session.SessionManager;
import com.kj.tms.common.utils.constant.Consts;
import com.kj.tms.pri.system.menu.dto.CommonModuleDto;
import com.kj.tms.pri.system.menu.dto.ModuleDto;
import com.kj.tms.pri.system.menu.service.CommonModuleService;
import com.kj.tms.pri.system.user.dto.UserDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wangguoqing
 * @Description:
 * @Date: Created in 17:02 2017/10/30
 * @Company: 北京宏图天安
 * @Version: 2017/10/30
 * @Modified By:
 */
@Controller
@RequestMapping("/system/syswelcome")
public class SysWelcomeController {
    private static Logger logger = LoggerFactory.getLogger(SysWelcomeController.class);
    @Autowired
    private SessionManager sessionManager;

    @Autowired
    private CommonModuleService commonModuleServiceImpl;

    /**
     * @return void 返回类型
     * @throws
     * @Title: getTreeData
     * @Description: 查询菜单模块树
     */
    @RequestMapping("/getCurrentMenu")
    @ResponseBody
    public ReturnData getTreeData() throws Exception {
        return new ReturnData(Consts.SUCCESS, "success", commonModuleServiceImpl.getCurrentMenu());
    }

    /**
     * @param commonModuleDto
     * @return void 返回类型
     * @throws
     * @Title: listCurrentCommonMenu
     * @Description: 查询当前常用功能列表数据
     */
    @RequestMapping("/listCurrentCommonMenu")
    @ResponseBody
    public ReturnData listCurrentCommonMenu(CommonModuleDto commonModuleDto) {
        logger.info("#################sessison id :" + sessionManager.getId());
        UserDto user = (UserDto) sessionManager.getAttribute(Consts.USER);
        commonModuleDto.setUserId(user.getUserId());
        commonModuleDto.setOrgCode(user.getCurUserOrgCode());
        List<ModuleDto> dataList = null;
        try {
            dataList = commonModuleServiceImpl.listCurrentCommonMenu(commonModuleDto);
        } catch (Exception e) {
            return new ReturnData(Consts.ERROR, "查询当前常用功能列表数据失败", "");
        }
        //设置页面数据
        Map<String, Object> returnObject = new HashMap<String, Object>();
        returnObject.put("rows", dataList);
        return new ReturnData(Consts.SUCCESS, "", returnObject);
    }

    /**
     * @param commonModuleDto
     * @param menuCodes       多个menuCode  以','分开
     * @return void 返回类型
     * @Title: saveModuleCommon
     * @Description: 添加常用功能
     */
    @RequestMapping("/saveModuleCommon")
    @ResponseBody
    public ReturnData saveModuleCommon(CommonModuleDto commonModuleDto,
                                       @RequestParam(value = "menuCodes",required = false) String menuCodes) {
        try {
            commonModuleServiceImpl.saveModuleCommon(menuCodes, commonModuleDto);
        } catch (Exception e) {
            return new ReturnData(Consts.ERROR, "常用功能替换失败", null);
        }
        return new ReturnData(Consts.SUCCESS, "常用功能替换成功", null);
    }
    /**
     * @param
     * @param
     * @return void 返回类型
     * @Title: saveModuleCommon
     * @Description: 添加常用功能
     */
    @RequestMapping("/index_right")
    public String indexRight(Model model, HttpServletRequest request ) {

//        model.addAttribute("menuCode",menuCode);

        return "/system//index_right";
    }

}
